<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh;">
      <t-form ref="form" :colon="true" :data="formData" labelWidth="130px" :rules="rules">
        <t-form-item v-if="objId" label="单位" name="departmentId">
          <t-tree-select clearable  filterable :treeProps="{keys: {value: 'id'}}" v-model="formData.departmentId"
                         :data="menuOptions"
                         placeholder="选择单位"></t-tree-select>
        </t-form-item>
        <t-form-item v-else label="单位" name="departmentIds">
          <t-tree-select clearable multiple filterable :treeProps="{keys: {value: 'id'},valueMode:'all'}" v-model="formData.departmentIds"
                         :data="menuOptions"
                         placeholder="选择单位"></t-tree-select>
        </t-form-item>
        <!--      <t-form-item label="类别名称" name="name">-->
        <!--        <t-input clearable v-model="formData.name" :min="0" placeholder="请输入"></t-input>-->
        <!--      </t-form-item>-->
        <!--      <t-form-item label="类型" name="categoryType">-->
        <!--        <t-radio-group v-model="formData.categoryType" :options="categoryTypeOptions"></t-radio-group>-->
        <!--      </t-form-item>-->
        <!--      <t-form-item v-if="formData.categoryType==='1'" label="申报时间" name="dateRange">-->
        <!--        <t-date-range-picker v-model="formData.dateRange" :placeholder="['开始时间','截止时间']" clearable/>-->
        <!--      </t-form-item>-->
        <t-form-item layout="inline">
          <t-form-item v-if="formData.categoryType==='1'" label="申报开始时间" name="startDate">
            <t-date-picker clearable v-model="formData.startDate" placeholder="申报开始时间"/>
          </t-form-item>
          <t-form-item v-if="formData.categoryType==='1'" label="申报截止时间" name="endDate">
            <t-date-picker clearable v-model="formData.endDate" placeholder="申报截止时间"/>
          </t-form-item>
        </t-form-item>
        <t-form-item layout="inline">
          <t-form-item v-if="formData.categoryType==='1'" label="大纲开始时间" name="dgStartDate">
            <t-date-picker clearable v-model="formData.dgStartDate" placeholder="大纲开始时间"/>
          </t-form-item>
          <t-form-item v-if="formData.categoryType==='1'" label="大纲截止时间" name="dgEndDate">
            <t-date-picker clearable v-model="formData.dgEndDate" placeholder="大纲截止时间"/>
          </t-form-item>
        </t-form-item>
        <t-form-item layout="inline">
          <t-form-item v-if="formData.categoryType==='1'" label="任务书开始时间" name="rwsStartDate">
            <t-date-picker clearable v-model="formData.rwsStartDate" placeholder="任务书开始时间"/>
          </t-form-item>
          <t-form-item v-if="formData.categoryType==='1'" label="任务书截止时间" name="rwsEndDate">
            <t-date-picker clearable v-model="formData.rwsEndDate" placeholder="任务书截止时间"/>
          </t-form-item>
        </t-form-item>
        <t-form-item layout="inline">
          <t-form-item v-if="formData.categoryType==='1'" label="验收开始时间" name="ysStartDate">
            <t-date-picker clearable v-model="formData.ysStartDate" placeholder="验收开始时间"/>
          </t-form-item>
          <t-form-item v-if="formData.categoryType==='1'" label="验收截止时间" name="ysEndDate">
            <t-date-picker clearable v-model="formData.ysEndDate" placeholder="验收截止时间"/>
          </t-form-item>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import {TODAY} from '@/utils/date';


export default {
  name: "add-edit-unit-plan",
  dicts: ["category_type"],
  components: {},
  props: ["header", "objId", "parentId"],
  data() {
    return {
      visible: true,
      loading: false,
      menuOptions: [],
      formData: {
        parentId: null,
        departmentId: null,
        departmentIds: [],
        name: "",
        sort: 0,
        dateRange: [],
        categoryType: '1',
        note: '',
        startDate: TODAY,
        endDate: TODAY,
        dgStartDate: '',
        dgEndDate: '',
        rwsStartDate: '',
        rwsEndDate: '',
        ysStartDate: '',
        ysEndDate: '',
      },
      rules: {
        startDate: [
          {
            required: true,
            message: "申报开始时间不能为空",
          },
        ],
        departmentId:[
          {
            required: true,
            message: "单位不能为空",
          },
        ],
        endDate: [
          {
            validator: (val) => {
              if (val && this.formData.startDate) {
                const startTime = new Date(this.formData.startDate).getTime()
                const endTime = new Date(val).getTime()
                return startTime <= endTime;
              }
              return true;
            }, message: "申报开始时间不能大于申报截止时间", type: "error"
          },
        ],
        dgEndDate: [
          {
            validator: (val) => {
              if (val && this.formData.dgStartDate) {
                const startTime = new Date(this.formData.dgStartDate).getTime()
                const endTime = new Date(val).getTime()
                return startTime <= endTime;
              }
              return true;
            }, message: "大纲开始时间不能大于大纲截止时间", type: "error"
          },
        ],
        rwsEndDate: [
          {
            validator: (val) => {
              if (val && this.formData.rwsStartDate) {
                const startTime = new Date(this.formData.rwsStartDate).getTime()
                const endTime = new Date(val).getTime()
                return startTime <= endTime;
              }
              return true;
            }, message: "任务书开始时间不能大于任务书截止时间", type: "error"
          },
        ],
        ysEndDate: [
          {
            validator: (val) => {
              if (val && this.formData.ysStartDate) {
                const startTime = new Date(this.formData.ysStartDate).getTime()
                const endTime = new Date(val).getTime()
                return startTime <= endTime;
              }
              return true;
            }, message: "验收开始时间不能大于验收截止时间", type: "error"
          },
        ],
        name: [{required: true, message: "类别名称不能为空"}],
        departmentIds: [
          {required: true, validator: (val) => val.length > 0, message: "单位不能为空", type: "error"}
        ]
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  created() {
    // if (!this.objId) {
    //
    // }
    this.getTreeselect();
  },
  methods: {
    getObj() {
      this.$api.kjxm.project.deptTimeConfigGet(this.objId).then((response) => {
        Object.assign(this.formData, response.data);
      });
    },
    getTreeselect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.menuOptions = response.data
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading =true;
          if (this.objId) {
            this.$api.kjxm.project.deptTimeConfigUpdate(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading =false;
            }).catch((e) => {
              this.loading =false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.kjxm.project.deptTimeConfigAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
